package com.linrui.user.controller;

import com.linrui.user.dto.ChangePasswordDto;
import com.linrui.user.dto.UserRegisterDto;
import com.linrui.user.dto.UserbinderWechatDto;
import com.linrui.user.service.IUserService;
import com.linrui.user.domain.User;
import com.linrui.user.query.UserQuery;
import com.linrui.basic.utils.AjaxResult;
import com.linrui.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "管理controller",description = "管理controller")
public class UserController {
    @Autowired
    public IUserService userService;

    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "保存")
    public AjaxResult save(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.add(user);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            userService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败！"+e.getMessage());
        }
    }
    // 批量操作 @TODO
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
                userService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情")
    public AjaxResult getById(@PathVariable("id")Long id)
    {
        try {
            User user = userService.getById(id);
            return AjaxResult.me().setResult(user);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的信息
    * @return
    */
    @GetMapping
    @ApiOperation(value = "查询全部")
    public AjaxResult getAll(){

        try {
            List< User> list = userService.getAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody UserQuery query)
    {
        try {
            PageList<User> pageList = userService.pageList(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取数据失败！"+e.getMessage());
        }
    }
    @PostMapping("/register/phone")
    public AjaxResult phoneRegister(@RequestBody UserRegisterDto dto){
        try {
            userService.registerPhone(dto);
            return AjaxResult.me().setMessage("注册成功");
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("注册失败，请重新注册");
        }
    }
    @PostMapping("/bind/wechat")
    public AjaxResult UserbinderWechat(@RequestBody UserbinderWechatDto dto){
        try {
            return userService.UserbinderWechat(dto);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("注册失败，请重新注册");
        }
    }
    @PostMapping("/changePassword")
    public AjaxResult findPassword(@RequestBody ChangePasswordDto dto){
        try {
            return userService.changePassword(dto);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("注册失败，请重新注册");
        }
    }
}
